package communicationMessages;

import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;

import serverComuncation.Observer;

import logger.LoggerWriter;
import data.Forum;
import data.ForumMessage;
import data.MemberDataBase;
import data.SubForum;
import database.DB;

public class EditMsgMessage extends Message {

	@Override
	public String getAnswer() {
		LoggerWriter logger = LoggerWriter.getLoggerWriter();
		String ans ="MsgEdited"+delimiter ;
		
		String copyStr=this.content;
		String [] arr =new String [7];
		int endIndex,beginIndex;
		beginIndex=0;
		for(int i=0; i<7;i++){
			endIndex=copyStr.indexOf(delimiter);
			arr[i]=copyStr.substring(beginIndex, endIndex);
			copyStr=copyStr.substring(endIndex+1);
		}	
		if (arr[4].equals("") && arr[5].equals(""))
		{
			logger.log("failed"+delimiter+"EditMsg Message wrong format"+delimiter, Level.WARNING);
			ans = "failed"+delimiter+"EditMsg Message wrong format"+delimiter;
		}
		else
		{
			String userName, subForumId ,threadId, messageId , newSubject ,newBody,strIsThread ;
			userName = arr[0];
			subForumId = arr[1];
			threadId = arr[2];
			messageId = arr[3];
			newSubject = arr[4];
			newBody = arr[5];
			strIsThread = arr[6];
			if (ForumMessage.isLegalMsg(newSubject, newBody) && DB.isSubForumExist(subForumId) && DB.isThreadExist(threadId))
			{
				Vector<String> path = new Vector<String>();
				path.add(subForumId);
				path.add(threadId);
				boolean isThread = true;
				boolean success = false;
				isThread = addToPathPostMsg(threadId, messageId, path, isThread,strIsThread);
				String userRoll = DB.getUserRoll(userName, path);
				int userId = DB.getUserId(userName);
				if (isAllowed(userName, subForumId, userRoll) && !DB.isUserBannedFromSubForum(userId, subForumId))
				{
					success= editMsg(threadId, messageId, newSubject, newBody, isThread);
				}
				
				if (success)
				{
					addToBannedUsersIfNeeded(userName, userId, subForumId);
					ans += userRoll+delimiter+"Success"+delimiter;
					String msg = "popupMessage"+delimiter+userName+delimiter+newSubject+delimiter+newBody+delimiter;
					String webMsg = null;
					if (isThread)
					{
						webMsg = this.getAddThreadPopupMsg(Integer.parseInt(threadId));
						
						try {
							sendRefreshMsg(logger, subForumId,Integer.parseInt(threadId),false);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					else
					{
						int postId = Integer.parseInt(messageId);
						webMsg = this.getAddPostWebPopupMsg(postId);
						sendRefreshPostMsg(threadId,postId);
					}
					this.sendMsgToObserver(subForumId, msg,webMsg);
				}
				else
				{
					ans += userRoll+delimiter+"Failed"+delimiter;
				}
				
			}
				
		}
		return ans;
	}

	private boolean isAllowed(String userName, String subForumId,
			String userRoll) {
//		return userRoll.equals("admin")||userRoll.equals("owner")||
//				(userRoll.equals("moderator") && DB.checkMoreThanAmonth(userName,subForumId));
		return userRoll.equals("owner");
	}

	private boolean addToPathPostMsg(String threadId, String messageId,
			Vector<String> path, boolean isThread,String strIsThread) {
		if (strIsThread.equals("false"))
		{
			isThread = false;
			path.add(messageId);
		}
		return isThread;
	}

	private boolean editMsg(String threadId, String messageId, String newSubject,
			String newBody, boolean isThread) {
		boolean success =false;
		if (isThread)
		{
			DB.editThreadMsg(threadId,newSubject,newBody);
			success = true;
		}
		else if (DB.isPostMsgExist(messageId))
		{
			DB.editPostMsg(messageId,newSubject,newBody);
			success = true;
		}
		return success;
	}
}

//String userName, subForumId ,threadId, messageId , newSubject ,newBody ;
//userName = arr[0];
//subForumId = arr[1];
//threadId = arr[2];
//messageId = arr[3];
//newSubject = arr[4];
//newBody = arr[5];
//int subForumInt = Integer.valueOf(subForumId);
//Vector<Integer> path = new Vector<Integer>();
//path.add(subForumInt);
//Forum forum = Forum.getForum();
//SubForum subForum = forum.getSubForum(subForumInt);
//int userId = MemberDataBase.getMembersDataBase().getMember(userName).get_id();
///*System.out.println("user name  "+userName+" user id:  "+userId);
//System.out.println("sub forum id  "+subForumId);
//System.out.println("thread id  "+threadId);
//System.out.println("msg id  "+messageId);
//System.out.println("new subject  "+newSubject);
//System.out.println("new body  "+newBody);*/
//if(subForum.editMessage(userId, messageId, threadId, newSubject,newBody))
//{
//	ans += MemberDataBase.getUserRoll(userName)+delimiter+"Success"+delimiter;
//	Observer.addNewMsg("popupMessage"+delimiter+userName+delimiter+newSubject+delimiter+newBody+delimiter);
//}
//else ans += MemberDataBase.getUserRoll(userName)+delimiter+"Failed"+delimiter;
